package com.gitee.funcy.multiple.datasource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.gitee.funcy.multiple.datasource.domain.App;
import com.gitee.funcy.multiple.datasource.domain.User;
import com.gitee.funcy.multiple.datasource.domain.query.AppQuery;
import com.gitee.funcy.multiple.datasource.mapper.AppMapper;
import com.gitee.funcy.multiple.datasource.mapper.UserMapper;
import com.gitee.funcy.multiple.datasource.service.IAppService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ｛这里添加描述｝
 *
 * @author fangchengyan
 * @date 2019-09-30 9:57
 */
@Service
public class AppServiceImpl implements IAppService {

    @Autowired
    private AppMapper appMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<App> selectByQuery(AppQuery query) {
        QueryWrapper<App> queryWrapper = new QueryWrapper<>();
        if(null != query.getId()) {
            queryWrapper.eq("id", query.getId());
        }
        if(StringUtils.isNotBlank(query.getAppName())) {
            queryWrapper.eq("app_name", query.getAppName());
        }
        if(StringUtils.isNotBlank(query.getAppNameLike())) {
            queryWrapper.like("app_name", query.getAppName());
        }
        if(StringUtils.isNotBlank(query.getDataSource())) {
            queryWrapper.like("data_source", query.getDataSource());
        }
        return appMapper.selectList(queryWrapper);
    }

    @Override
    public App selectById(Long id) {
        return appMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(String appName, String dataSource) {
        App entity = new App();
        entity.setAppName(appName);
        entity.setDataSource(dataSource);
        int result = appMapper.insert(entity);
        throw new RuntimeException("这里抛出一个异常");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveMix(String appName, String dataSource, String nick) {
        App app = new App();
        app.setAppName(appName);
        app.setDataSource(dataSource);
        int result1 = appMapper.insert(app);
        User user = new User();
        user.setNick(nick);
        int result2 = userMapper.insert(user);
        throw new RuntimeException("这里抛出一个异常");
    }


}
